package com.ciei.dpagm.service;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.enums.ModelModuleTypeEnum;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.util.ApplicationContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

@Service
public class ModelModuleService implements IModelModuleService{

    /**
     * 加载数据模型列表
     * @param data
     * @return
     */
    public JSONObject loadModelList(JSONObject data){
        Integer type = data.getInteger("type");
        IModelService modelService = getModelService(type);
        return modelService.loadModelList();
    }

    /**
     * 加载数据模块列表
     * @param data
     * @return
     */
    public JSONObject loadModuleList(JSONObject data){
        IModuleService moduleService = getModuleService(data.getInteger("type"));
        return moduleService.loadModuleList(data.getInteger("pid"));
    }

    /**
     * 搜索模型模块数据
     * @param data
     * @return
     */
    public JSONObject searchDataByName(JSONObject data){
        IModelService modelService = getModelService(data.getInteger("type"));
        return modelService.searchDataByName(data.getString("searchName"));
    }

    /**
     * 添加模型或模块
     * @param data
     * @return
     */
    @Override
    public JSONObject add(JSONObject data){
        Integer type = data.getInteger("type");
        IModelModuleService modelModuleService = getModelModuleService(type,data.getInteger("pid"));
        return modelModuleService.add(data);
    }

    @Override
    public JSONObject update(JSONObject data) {
        Integer type = data.getInteger("type");
        IModelModuleService modelModuleService = getModelModuleService(type,data.getInteger("pid"));
        return modelModuleService.update(data);
    }

    @Override
    public JSONObject delete(JSONObject data) {
        Integer type = data.getInteger("type");
        IModelModuleService modelModuleService = getModelModuleService(type,data.getInteger("pid"));
        return modelModuleService.delete(data);
    }

    @Override
    public JSONObject copy(JSONObject data, Users user) {
        Integer type = data.getInteger("type");
        IModelModuleService modelModuleService = getModelModuleService(type,data.getInteger("pid"));
        return modelModuleService.copy(data,user);
    }

    @Override
    public JSONObject export(JSONObject data, HttpServletResponse response) {
        IModelModuleService modelModuleService = getModelModuleService(data.getInteger("type"),data.getInteger("pid"));
        return modelModuleService.export(data, response);
    }

    public IModelService getModelService(Integer type){
        return  (IModelService) ApplicationContextHolder.getBean(ModelModuleTypeEnum.typeValueOf(type).getName() + "ModelService");
    }

    public IModuleService getModuleService(Integer type){
        return  (IModuleService) ApplicationContextHolder.getBean(ModelModuleTypeEnum.typeValueOf(type).getName() + "ModuleService");
    }

    public IModelModuleService getModelModuleService(Integer type,Integer pid){
        if (pid == null){
            return getModelService(type);
        }
        return  getModuleService(type);
    }

}
